package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.service.CheckGroupService;
import com.itheima.service.MemberService;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 会员服务
 */
@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    @Override
    public Member findByTelephone(String Email) {
        return memberDao.findByTelephone(Email);
    }


    //保存会员信息
    @Override
    public void add(Member member) {
        if(member.getPassword() != null){
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberDao.add(member);
    }

    //根据月份统计会员数量
    @Override
    public List<Integer> findMemberCountByMonth(List<String> mounths) {
        List<Integer> list = new ArrayList<>();
        for(String m : mounths){
            String[] split = m.split("\\.");//分割成年 和 月  格式：2021.04
            int year = Integer.parseInt(split[0]);//获取年
            int month= Integer.parseInt(split[1]);//获取月

            if (month == 2){//判断是否是2月
                if(year % 4 != 0 || year % 100 == 0 && year % 400 != 0) {//判断是否是 平年还是闰年
                    m = m + ".28";    //格式：20xx.02.28
                }else {
                    m = m + ".29";
                }
            }else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12){//1-3-5-7-8-10-12有31天，2月除外，剩下30天
                m = m + ".31";
            }else if(month == 4 || month == 6 || month == 9 || month == 11 ) {//4、6、9、11 是30天
                m = m + ".30";
            }

            Integer count = memberDao.findMemberCountBeforeDate(m);
            list.add(count);
        }
        return list;
    }
}
